<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use EasyWeChat\Factory;
use app\common\model\RedEnvelopes as RedEnvelopesModel;
use app\common\model\MoneyLog;
use think\Db;


class RedEnvelopes extends Api
{

    protected $noNeedLogin = ['getVideoList'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }

    /**
     * 获取红包
     */
    public function get()
    { //video_id
        $video_id = $this->request->post('video_id/d');
        $result = RedEnvelopesModel::field('id ,price')->where('video_id', $video_id)->where('stock', '>', 0)->find();
        if (!$result) {
            $this->error("暂无红包!");
        }
        //判断是否领取过红包,每天只能领一次
        $start_time = strtotime(date('Y-m-d'));
        $end_time =  $start_time + 86400;
        $video = MoneyLog::where('video_id', $video_id)->where('type', 1)->where('user_id', $this->auth->id)->where('createtime','>=',$start_time)->where('createtime','<=',$end_time)->find();
        if ($video) {
            $this->error("此红包今天已经领取!");
        }
        Db::startTrans();
        try {
           // \app\common\model\User::where('id', $this->auth->id)->setInc('money', $result['price']);
            \app\common\model\RedEnvelopes::where('id', $result->id)->setDec('stock', 1);
            (new MoneyLog)->saveUserMoney(1, 1, $this->auth->id, $result['price'], "领取红包", $video_id);
            Db::commit();
            $this->success(__('领取红包成功'), [$result]);
        } catch (\think\Exception\DbException $e) {
            Db::rollback();
            return $this->error('操作失败');
        }
    }

    /**
     * 收益明细
     */
    public function getList()
    {

        $where = [
            'page' => $this->request->post('page/d', 1),
            'limit' => $this->request->post('limit/d', 10),
            'uid' => $this->auth->id,
            'type' => $this->request->post('type/d', 1)
        ];
        $ret = \app\common\model\MoneyLog::systemPage($where);
        foreach ($ret['list'] as $key => $val) {
            $ret['list'][$key]['createtime'] = date('Y-m-d H:i:s', $val['createtime']);
        }
        if ($ret) {
            $this->success(__('数据获取成功'), $ret);
        } else {
            $this->error($this->auth->getError());
        }
    }

    public function getVideoList()//加一个红包是否领取
    {

        $where = [
            'page' => $this->request->post('page/d', 1),
            'limit' => $this->request->post('limit/d', 10),
        ];
        $ret = \app\common\model\Video::systemPage($where);
        foreach ($ret['list'] as $key => $val) {
            $red = \app\common\model\RedEnvelopes::where('video_id', $val['id'])->find();
            if ($red) {
                $ret['list'][$key]['red_price'] = $red['price'];
            } else {
                $ret['list'][$key]['red_price'] = "";
            }
            
           $start_time = strtotime(date('Y-m-d'));
           $end_time =  $start_time + 86400;
           $video = MoneyLog::where('video_id', $val['id'])->where('type', 1)->where('user_id', $this->auth->id)->where('createtime','>=',$start_time)->where('createtime','<=',$end_time)->find();            
           if($video) {
             $ret['list'][$key]['red_envelopes'] = 1;
            }else{
             $ret['list'][$key]['red_envelopes'] = 0;   
            } 
            $ret['list'][$key]['createtime'] = date('Y-m-d H:i:s', $val['createtime']);
            $ret['list'][$key]['video_url'] = "http://47.97.173.232" . $val['video_url'];
            $ret['list'][$key]['video_image'] = "http://47.97.173.232" . $val['video_image'];
        }
        if ($ret) {
            $this->success(__('数据获取成功'), $ret);
        } else {
            $this->error($this->auth->getError());
        }
    }
}
